<template>
	<view>
		<view class="container">
			<view class="s-searchbox">
				<view class="s-search-input">
					<s-icon name="sousuo" color="#999" :size="44"></s-icon>
					<input confirm-type="search" :placeholder="$s.config.getConfig('PleaseSearchInformation')" :focus="true" auto-focus placeholder-class="s-input-plholder"
					 class="s-input" v-model.trim="key" @confirm="back"/>
				</view>
				<view class="s-cancle" @tap="back">确定</view>
			</view>
			<view>
				<view class="s-history-header" v-if="consult_search_arr.length>0">
					<view class="s-search-title">搜索历史</view>
					<s-icon name="shanchu" color="#000000" :size="44" @tap="delete_search"></s-icon>
				</view>
				<view class="s-history-content">
					<view v-for="(item,index) in consult_search_arr" :key="index" class="s-tag-class" @tap="consult_list(item)">{{item}}</view>
				</view>
			</view>
		</view>	
	</view>
</template>

<script>  
	export default {
		data() {
			return {
				//搜索内容
				key:'',
				consult_search_arr: []
			}
		},
		async onShow() {
			await this._init()
		},
		methods: {
			async _init(){
				const consult_search_arr = await this.$s.uniApi.getStorage('consult_search_arr')
				this.consult_search_arr = consult_search_arr.data || []
			},
			//删除搜索
			async delete_search(){
				const res = await this.$s.uniApi.showModal({content:'确定要删除搜索历史吗？'})
				if (res.confirm) {
					await this.$s.uniApi.removeStorage('consult_search_arr')
					this.consult_search_arr = []
				}
			},
			//点击搜索
			back(){
				if(this.key != ''){
					 if(this.consult_search_arr.length>0){
						this.consult_search_arr.unshift(this.key)
					 }else{
						this.consult_search_arr = [this.key]
					 }
					 this.consult_search_arr = this.$s.api.uniqueArr(this.consult_search_arr).slice(0,10)
					 this.$s.uniApi.setStorage('consult_search_arr',this.consult_search_arr)
					 this.$s.uniApi.navigateTo({url:'../consult_list/consult_list?key='+this.key})
				}
			},
			consult_list(item){
				this.$s.uniApi.navigateTo({url:'../consult_list/consult_list?key='+item})
			}
		},
		//下拉刷新
		async onPullDownRefresh() {
			await this._init()
			uni.stopPullDownRefresh()
		}
	}
</script>
<style lang="scss" scoped>
@import 'consult_search.scss';
</style>